/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * edit_menu.java
 *
 * Created on Jul 25, 2011, 1:28:17 AM
 */
package introse1;

import javax.swing.table.*;
import javax.swing.table.TableModel.*;
import javax.swing.JTable.*;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.PreparedStatement;
/**
 *
 * @author Trisha
 */
public class edit_menu extends javax.swing.JFrame {

    /** Creates new form edit_menu */
    public edit_menu(manage_menu manage_menu) {
        initComponents();
        int x=-1;
        DBConnect conn = new DBConnect();
        Connection pangkonek = conn.connect();
        this.manage_menu = manage_menu;
        VariableList lists = manage_menu.getListahan();
        Name.setText(lists.getName_of_food());
        FoodName.setText(lists.getName_of_food());
        s3 = lists.getName_of_food();
        PreparedStatement pstmt;
        ResultSet set;
        ResultSet res;
        DefaultTableModel tmodel = (DefaultTableModel)EditIngredientTable.getModel();
        try{
            float price = -1;
            pstmt = pangkonek.prepareStatement("Select * FROM recipe_file WHERE name_of_food = ?");
            pstmt.setString(1, lists.getName_of_food());
            res = pstmt.executeQuery();
            while(res.next()){
                x = Integer.parseInt(res.getString("recipe_id"));
                price = Float.parseFloat(res.getString("price"));
            }
            FoodPrice.setText(Float.toString(price));
            pstmt = pangkonek.prepareStatement("Select * FROM recipe_ingredient WHERE recipe_id = ?");
            pstmt.setInt(1, x);
            res =  pstmt.executeQuery();
            while(res.next()){
                pstmt = pangkonek.prepareStatement("Select inventoryName FROM inventory WHERE inventoryID=?");
                pstmt.setInt(1,Integer.parseInt(res.getString("inventory_id")));
                set = pstmt.executeQuery();
                if(set.next()){
                    String[] a = {set.getString("inventoryName"), res.getString("quantity")};
                    tmodel.addRow(a);
                }
            }
        }catch(Exception e){}
        DefaultTableModel dtmodel = (DefaultTableModel)IngredientTable.getModel();
        try{
            res = Manage.getIngredients();
            while(res.next()){
                String[] s1 = {res.getString("inventoryName")};
                dtmodel.addRow(s1);
            }
        }catch(Exception e){
                
        }
        manage_menu.setVisible(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        FoodName = new javax.swing.JTextField();
        FoodPrice = new javax.swing.JTextField();
        SaveButton = new javax.swing.JButton();
        BackButton = new javax.swing.JButton();
        Name = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        EditIngredientTable = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        IngredientTable = new javax.swing.JTable();
        AddRecipeButton = new javax.swing.JButton();
        DeleteIngredient = new javax.swing.JButton();
        RecipeTableError = new javax.swing.JLabel();
        FoodError = new javax.swing.JLabel();
        PriceError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Rockwell", 0, 24));
        jLabel1.setText("Edit Menu");

        jLabel2.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel2.setText("Name of Food:");

        jLabel3.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel3.setText("Price of Food:");

        FoodName.setEditable(false);
        FoodName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FoodNameActionPerformed(evt);
            }
        });

        SaveButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        SaveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/save.png"))); // NOI18N
        SaveButton.setBorder(null);
        SaveButton.setBorderPainted(false);
        SaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveButtonActionPerformed(evt);
            }
        });

        BackButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        BackButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/back.png"))); // NOI18N
        BackButton.setBorder(null);
        BackButton.setBorderPainted(false);
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackButtonActionPerformed(evt);
            }
        });

        EditIngredientTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ingredient", "Quantity", "Measure"
            }
        ));
        jScrollPane2.setViewportView(EditIngredientTable);

        IngredientTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
            },
            new String [] {
                "Ingredient"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        IngredientTable.setSelectionMode(1);
        jScrollPane1.setViewportView(IngredientTable);

        AddRecipeButton.setText("  >>>> ");
        AddRecipeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddRecipeButtonActionPerformed(evt);
            }
        });

        DeleteIngredient.setText("  <<<< ");
        DeleteIngredient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteIngredientActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabel1)
                        .addGap(26, 26, 26)
                        .addComponent(Name, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(SaveButton)
                        .addGap(18, 18, 18)
                        .addComponent(BackButton))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(FoodPrice, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(FoodName, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(AddRecipeButton)
                                    .addComponent(DeleteIngredient))
                                .addGap(29, 29, 29)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(RecipeTableError, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(PriceError, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(FoodError, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(Name))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(FoodName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(FoodPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(FoodError, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(PriceError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(RecipeTableError)
                        .addGap(53, 53, 53))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(AddRecipeButton)
                        .addGap(27, 27, 27)
                        .addComponent(DeleteIngredient)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BackButton)
                    .addComponent(SaveButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonActionPerformed
        // TODO add your handling code here:
        int x = EditIngredientTable.getRowCount();
        vars.setName_of_food(FoodName.getText());
        
            try{
                vars.setPrice_of_food(Float.parseFloat(FoodPrice.getText()));
            }catch(NumberFormatException e){
                PriceError.setText("Input must be a number");
            }
            if(x > 0){
                Manage.Deletetorecipelist(this, s3);
                Manage.EditToMenu(this);
                for(int i = 0; i < x; i++){
                    String s1 = (String)EditIngredientTable.getModel().getValueAt(i, 0).toString();
                    Float f2 = Float.parseFloat((String)EditIngredientTable.getModel().getValueAt(i,1).toString());
                    vars.setIngredient_name(s1);
                    vars.setRecipe_quantity(f2);
                    Manage.Edittorecipelist(this);
                }
                manage_menu.setTable(vars);
                //To DO: add prompt here to confirm 
            }
            else{
               RecipeTableError.setText("No ingredients");
            }
        
    }//GEN-LAST:event_SaveButtonActionPerformed

    private void BackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackButtonActionPerformed
        this.setVisible(false);
        manage_menu.setVisible(true);
    }//GEN-LAST:event_BackButtonActionPerformed

    private void FoodNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FoodNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_FoodNameActionPerformed

    private void AddRecipeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddRecipeButtonActionPerformed
        try{
            String str = (String)IngredientTable.getModel().getValueAt(IngredientTable.getSelectedRow(), 0);
            vars.setIngredient_name(str);
            String name = str;
            this.setVisible(false);
            edit_recipe next = new edit_recipe(this, name);
            next.setVisible(true);
        }catch(Exception e){
            System.out.println("woahhh");
        }
}//GEN-LAST:event_AddRecipeButtonActionPerformed

    private void DeleteIngredientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteIngredientActionPerformed
        // TODO add your handling code here:
        try{
            DefaultTableModel tmodel = (DefaultTableModel)EditIngredientTable.getModel();
            
            int x = EditIngredientTable.getSelectedRow();
            tmodel.removeRow(x);
        }catch(Exception e){
            RecipeTableError.setText("No Row Selected");
        }
    }//GEN-LAST:event_DeleteIngredientActionPerformed

    public void setVars(VariableList list){
        vars = list;
    }
    
    public VariableList getVars(){
        return vars;
    }
    public void setRecipeTable(){
       DefaultTableModel tmodel = (DefaultTableModel)EditIngredientTable.getModel();
       String[] s1 = {vars.getIngredient_name(), Float.toString(vars.getRecipe_quantity()), vars.getUnit_of_measure()};
       tmodel.addRow(s1);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new edit_menu(manage_menu).setVisible(true);
            }
        });
    }
    public static VariableList vars = new VariableList();
    public static MenuManager Manage = new MenuManager();
    private static manage_menu manage_menu;
    public static String s3;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddRecipeButton;
    private javax.swing.JButton BackButton;
    private javax.swing.JButton DeleteIngredient;
    private javax.swing.JTable EditIngredientTable;
    private javax.swing.JLabel FoodError;
    private javax.swing.JTextField FoodName;
    private javax.swing.JTextField FoodPrice;
    private javax.swing.JTable IngredientTable;
    private javax.swing.JLabel Name;
    private javax.swing.JLabel PriceError;
    private javax.swing.JLabel RecipeTableError;
    private javax.swing.JButton SaveButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
